<?php
global $JoinYear;
global $PrevYear;
$Year = date("Y"); $NYear = date("Y")+1; $PYear = date("Y")-1;
$JoinYear = $Year."-".$NYear;
$PrevYear = $PYear."-".$Year;
$path = drupal_get_path('module', 'document');
drupal_add_css($path . '/css/documents.css');
//drupal_add_js($path . '/js/documents.js');

function documentscategoryhome() {
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>
			</div>
		    <div class="inner_li">
				<a class="view_head_sel "  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>
			<div class="inner_li">';
			if($language->language =='en'){
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>
		<div class="clear"></div>
	</div>';
	my_db_change(1);
	$result = db_query("select * from document_type_mst where doct_id in (select distinct(cast(field_doccategory_value as integer)) as cateidvalue from
						field_data_field_doccategory  a,node n,field_data_field_department d where a.entity_id=n.nid and
						 d.entity_id=n.nid and (n.language='".$language->language."' or n.language = '') and n.status=1) order by doct_id");
	my_db_change(0);
	$op = "<div class='document_head_list'>";
	$st =1;
	foreach ($result as $dept) {  //print "<pre>";print_r($items);print "</pre>";

	 	if($st%2!=0) {
			$cls = "first_clm";
		}
		else {
			$cls = "second_clm";
		}

		 $op .= "<div class='".$cls." d".$dept->doct_id."'>";
		 $op .= "<img src='".$url."/images/icon_main.png' alt='More' /> ";
		 $op .= l(trim(t($dept->doct_type_name)),$lang.'/documents/category/'.$dept->doct_id)."</div>";

		 $st++;

	}
	$op .="</div>";
	$output .= "<div class='result_inner'>";
	$output .= $op;
	$output .= "<div class='clr'></div></div>";
return $output;
}
function documentscatewise() {
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }

	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>
			</div>
		    <div class="inner_li">
				<a class="view_head_sel "  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>
			<div class="inner_li">';
			if($language->language =='en'){
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
	</div>';
	 my_db_change(1);
	 $catid = arg(2);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')
	  ->fieldCondition('field_doccategory', 'value', $catid, '=')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN')
	  ->pager(15,0);

	$result = $query->execute();


	$nid = current($result['node']);

	if (isset($result['node'])) {
	  $items = array_keys($result['node']);
	}
//print "<pre>";print_r($items);print "</pre>";
	 my_db_change(0);
	$output .= '<div class="result_inner">';
	$output .='<div class="res_dept"><span><strong>'.t(category_name($catid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span></div>';
	$output .= theme('documents_archive_theme',array('items' => $items,'depid'=>$depid,'nid'=>$nid));
	$output .='<div class = "clear"></div></div>';
	$output .=archive_year_cat();
	$output .= theme('pager');
return $output;
}
function documentscatearchivee(){
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 $catid = arg(2);
 $currentyr = arg(3);
  if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }

	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header view-archive">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>
			</div>
		    <div class="inner_li">
				<a class="view_head_sel"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>
			<div class="inner_li">';
			if($language->language =='en'){
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>';

	$output .='<div class="result_inner"><div class="res_dept"><div class="arcyear">'.t('Archive of the ').$currentyr.'</div>';
	$output .='<span><strong>'.t(category_name($catid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span></div>';
	my_db_change(1);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')
	  ->fieldCondition('field_doccategory', 'value', $catid, '=')
	  ->fieldCondition('field_doc_year', 'value', $currentyr, '=')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN')
	  ->pager(15,0);

	$result = $query->execute();


	$nid = current($result['node']);

	if (isset($result['node'])) {
	  $items = array_keys($result['node']);

	 // $items = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	$output .= theme('documents_archive_theme',array('items' => $items,'cat'=>$catid,'currentyr'=>$currentyr,'nid'=>$nid));
	$output .=archive_year_cat();
	$output .= theme('pager');

	return $output;
}
function archive_year_cat(){
global $language,$base_url,$user,$JoinYear,$PrevYear;
//print $JoinYear;
 $url = $base_url . '/' . drupal_get_path('module', 'documents');
   if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
    my_db_change(1);
    $catid = arg(2);
    $title = '';
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');

	$result = $query->execute();


	$nid = current($result['node']);

	if (isset($result['node'])) {
	    $news_items_nids = array_keys($result['node']);

		$news_items  = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
//print "<pre>";print_r($news_items);print "</pre>";
 	my_db_change(0);
 	$output .= '<div id="docArchive" style="text-align:right;"><span>Archive</span>';
	$i=0;
	foreach($news_items as $items){
		if($title != $items->field_doc_year['und'][0]['value']) {
			$title = $items->field_doc_year['und'][0]['value'];
			$output .= '<a  style="padding: 10px 5px;" href="'.$base_url.$lang.'/documents/category/'.$catid.'/'.$title.'">'.$title.'</a>';
			if($i=='5' || $i == '10' || $i == '15'){
				$output .= '<div class="clr" style="padding-top: 10px;"></div>';
			}
			$i++;
		}
	}
	$output .='</div>';
	return $output;

}
function documentsalpha($alp){
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');
$alp = arg(2);
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }

	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>
			</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>
			<div class="inner_li">';
			if($language->language =='en'){
		 		$output .= '<a class="view_head_sel" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>';
	$output .= "</div><div class='view-header'>" . AtoZ_letters($alp) . "</div>";
	$output .= "<div class='result_inner'>";
	$val= strtoupper($alp);

	 my_db_change(1);
		if($val != 'ALL'){

			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$val.'%','LIKE')
			  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0);
		 }
		 else {

			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')
			  ->propertyCondition('status',1)
			  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0);
		 }
		$result = $query->execute();


	$nid = current($result['node']);

	if (isset($result['node'])) {
	  $items = array_keys($result['node']);
	  // $items = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
my_db_change(0);
foreach ($items as $nid){
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$row = node_load($nid, $vid);
			
		 if ($row->field_doctype['und'][0]['value'] == 'online') {
			$link =  $row->field_docupload['und'][0]['value'];
		 }
		else if($row->field_doctype['und'][0]['value'] == 'download') {
		    $link = $row->field_docupload['und'][0]['uri'];		
		    $wrapper = file_stream_wrapper_get_instance_by_uri($link);
			if($wrapper == true){
				 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
			}
			$link="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
		}
		if ($row->field_docupload['und'][0]['filesize'] != '0') {
			$kb = ceil($row->field_docupload['und'][0]['filesize']/1024);
			$kbyte= $kb.KB;
			if($kb >1024) { 
				$kb = ceil($kb/1024);
				$kbyte= $kb.MB;
			}
		} my_db_change(0);

		$output .= '<div class="res_title_ch" style="">
				 <p><a onclick="return tnportal_window('.$link.');" href="javascript:void(0);" >
						'.ucfirst(t($row->title)) .'  <img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">
						<span class="kb">
							<span>('.$kbyte.')</span>
						</span>		
					</a>
				 </p>
			</div>	';		
		$i++; } 
		if($i == '0'){
			$output .=' <div class="no_record">No Results Found </div>';
		}
	$output .='</div>
</div>';
	$output .= theme('pager');
	return $output;
}
function documents_search_form(){
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');

 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }

	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="inactive" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>
			</div>
		    <div class="inner_li">
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>
			<div class="inner_li">';
			if($language->language =='en'){
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>
		    <div class="inner_li">
				<a class="view_head_sel"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div></div>';
	$form = drupal_get_form('documents_search');
	$output .= '<div class="result_inner_search">'. drupal_render($form);
	/*$output .= '<form id="document-search-form" method="post" accept-charset="UTF-8" action="'.$base_url.'/documents/search">
				<div><input type="hidden" value="documents" id="edit-hidden-content" name="hidden_content">
				<div id="edit-doc-cont-search-wrapper" class="form-item">
				 <label for="edit-doc-cont-search">Keyword: </label>
				 <input type="text" class="form-text" value="" size="35" id="edit-doc-cont-search" name="doc_cont_search" maxlength="128">
				 <div class="description">(Eg : Higher Education Policy Note)</div>
				</div>
				<div class="sea_go_but"><input type="submit" class="form-submit" value="Go" name="search">
				</div>
				<input type="hidden" value="document_search_form" id="edit-document-search-form" name="form_id">

				</div></form>';*/
	return $output;

}
function documents_search($form_state){
	$keyword = trim(arg(2));
	$form['documents_search'] = array(
	'#type' => 'textfield',
	'#title' => t('Keyword'),
	'#default_value' => $keyword ? $keyword : '',
	'#size'=>'35',
	'#description'=>'Eg: Handbook of Agriculture Department',
	);

	$form[ 'field1'] = array(
		'#prefix'=>'<div class="description"> (Eg:Handbook of Agriculture Department) </div>'
	);

    $form[ 'documents_submit' ] = array(
	'#title' => 'Submit',
	'#type' => 'submit',
	'#value' => 'Go',
	'#name' => 'search',
	'#prefix' => '<div class="sea_go_but">',
	'#suffix' => '</div>',

	);

	return $form;

}
function documents_search_submit($form,&$form_state){
	global $base_url;

	//print "form submit";

	 //print "<pre>"; print_r($form_state); print "</pre>";
	 //drupal_set_message(t('The form has been submitted.')); die;

	 	if ($form_state['clicked_button']['#name'] == 'search') {
			$str = $form_state['values']['documents_search'];
			if(empty($str)) {
			form_set_error('documents_search','Please Enter the Search Keyword');
			//echo 'Please Enter Key Word';
			}

		}


	$form_state['redirect'] = 'documents/search/'.$str;
    return;
}


function documents_search_result_form() {
global $language,$base_url;
$url = $base_url . '/' . drupal_get_path('module', 'document');
$output = documents_search_form();
$go_keyword= trim(arg(2));
$news_items_nids = array();
$nid = '';
$items = array();
	
		$go_keyword= strtolower($go_keyword);
		//$ser_string = array();
		$go_keyword = preg_replace('/[\p{Z}\s]{2,}/u', ' ', $go_keyword);
		$go_keyword=pg_escape_string($go_keyword);

		$len = drupal_strlen($go_keyword);
		$keyword1 =$go_keyword;

	$go_val = $go_keyword;
	my_db_change(1);
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')
			  ->propertyCondition('status',1)
			  ->propertyCondition('title','%'.$go_val.'%','LIKE')
			  //->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0);
	$result = $query->execute();
	

	if (isset($result['node'])) {
	  $items = array_keys($result['node']);
	  $nid = current($result['node']);
	  // $items = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
	$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'documents')
			  ->propertyCondition('status',1)
			  ->propertyCondition('title','%'.$go_val.'%','LIKE')
			 // ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC');
	$count = $query->count()->execute();
	my_db_change(0);
	if($count != '0') {
		$output .='<div class="clr"><div class="record_found">'.$count.' Records Found</div></div>';
	}
	//$output .= theme('documents_archive_theme',array('items' => $items,'nid'=>$nid));
	
my_db_change(0); $i=0;
foreach ($items as $nid){
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$row = node_load($nid, $vid); 
			//print "<pre>";print_r($row);print "</pre>";
		 if ($row->field_doctype['und'][0]['value'] == 'online') {
			 $link =$row->field_docurl['und'][0]['value'];
			 		 $link="'$link'";

		 }
		else if($row->field_doctype['und'][0]['value'] == 'download') {
		    $link = $row->field_docupload['und'][0]['uri'];		
		    $wrapper = file_stream_wrapper_get_instance_by_uri($link);
			if($wrapper == true){
				 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
			}
			$link="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
		}
		$kbyte='';
		if($row->field_docupload){
			if ($row->field_docupload['und'][0]['filesize'] != '0') {
				$kb = ceil($row->field_docupload['und'][0]['filesize']/1024);
				$kbyte= $kb.KB;
				if($kb >1024) { 
					$kb = ceil($kb/1024);
					$kbyte= $kb.MB;
				}
			}
		} my_db_change(0);

		$output .= '<div class="res_title_ch" style="">
				 <p><a onclick="return tnportal_window('.$link.');" href="javascript:void(0);" >
						'.ucfirst(t($row->title)) .'  ';
				if($kbyte !=''){
						$output .= '<img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif"><span class="kb">
							<span>('.$kbyte.')</span>
						</span>';		
				}
					$output .='</a>
				 </p>
			</div>	';		
		$i++; } 
		if($i == '0'){
			$output .=' <div class="no_record" style="padding-top:35px;">No Results Found </div>';
		}
	$output .='</div>
</div>';
	$output .= '</div>';
	$output .= theme('pager');
return $output;

}
function AtoZ_letters($alp){
	global $base_url;
		//print_r( strtolower($alpha ));
	for($i =65; $i < 91; $i++){
			$class = "";

// strtolower($val );
		if(chr($i) == $alp){
			$class = " view_head_sel";
		}
		else
		{
			$class = " ";
		}
		//$linkOp .= l($val,'documents/atoz/'.$val,array('attributes'=>array('class'=>$class)))."&nbsp;&nbsp;";
		 $linkOp .= '<a class = "myclass' . $class . '"  href = "' .$base_url.'/documents/atoz/'. chr($i) .'" >' . chr($i) . '</a>';
	}
	$class = "";
	if($alp == 'All' || empty($alp)){
		$class = " view_head_sel";
	}
	$linkOp .= '<a class = "myclass' . $class . '" href = "' .$base_url.'/documents/atoz/All" title = "All" id = "All"> All </a>';
	$StrOp = "<div class='result_inner_atoz'>";
	$StrEnd = "</div>";
	$Grey = "<div class='grey_bord_line'></div>";//$linkOp .="<br /><br />";
	return $StrOp . $linkOp . $Grey . $StrEnd;
}
?>